#! /usr/bin/python -S

########################################################
# Part of MILIM NIRDAFOT project.
# Written by H. Magal, 2014
# See details on the project at the site:
#   https://sites.google.com/site/nirdafotdownloads/home
########################################################

import sys
sys.setdefaultencoding("utf-8")         # this is needed for the windows.
import site                             # we call python with -S, so now we import the site module
import os
import argparse
import webbrowser
import multiprocessing
import GPLv3_license_acceptance
import constants
import setup

try:
    import wx
except:    # catch all errors. It might be that wxPython was not installed
    print ("Error: Cannot call wxPython module. Is it installed?")
    webbrowser.open ('https://sites.google.com/site/nirdafotdownloads/home/no_wx_installed')
    sys.exit (1)

import syng_main

###### Main code ######
def main():
    platform_detected = constants.set_platform_dependent_vars ()
    # command line args handling
    parser = argparse.ArgumentParser ()
    parser.add_argument ("--tech", help="Show the tech support menu", action='store_true') # args.tech gets the values True/false respectively
    args = parser.parse_args ()

    app = wx.App ()
    if platform_detected is False:
        GPLv3_license_acceptance.fatal_error_and_exit ('Fatal Error', 'Error: Cannot identify platform type')
    e = GPLv3_license_acceptance.check_existing_accept_file ()
    if not e is True:
        if (constants.platform_is_Win is True) and (constants.Win_uses_1folder is True):
            setup.showCustomDialog (None)       # check if system supports RTL. Is needed for the 1file window version
            # It is probably the first activation of windows-1folder. Check existance of dataBase, and import if not found
            if not os.path.isfile (constants.path_4_data_files + 'synonyms.data'):          # file DOESN'T exist
                site = setup.handleGoggleSiteFiles ()
                xml_info = site.parse_URL_file ('nirdafot.xml')
                if xml_info is None:
                    GPLv3_license_acceptance.fatal_error_and_exit ('Fatal Error', 'Error: Cannot access Internet')
                site_versions = xml_info [0]
                # we try to fetch dataBase without success/fail check
                site.update_database (site_versions[1].text)   # we need the new version to form the proper file name

        # next window displays to user the license and requests acceptance/refusal of terms of use
        lwindow = GPLv3_license_acceptance.MyLicFrame ()  # call class MyLicFrame
        app.MainLoop ()                 # start the event loop

    # main app & window handling
    syng_main.MyFrame (args.tech)       # call class MyFrame
    app.MainLoop ()                     # start the event loop


if __name__ == '__main__':
    #######################################################################################
    # this line is a must due to Python API docs.
    # see the page
    #  http://docs.python.org/2/library/multiprocessing.html#multiprocessing.freeze_support
    # It must be the first line after if __name__ == '__main__'
    multiprocessing.freeze_support()
    #######################################################################################
    # call main function
    main ()